# -*- coding: utf-8 -*-
# @Time : 2025/1/23 15:57
# @Author : debbyszhang
# @File : base_page.py
# @desc :
import re

from playwright.sync_api import Page


class BasePage:
    def __init__(self, page: Page):
        self.page = page

    def open(self, url: str):
        """打开指定的 URL"""
        self.page.goto(url)

    def radio(self, label):
        self.page.get_by_label(label).check()

    def click(self, label, role='button'):
        self.page.get_by_role(role, name=label).click()

    def click_text(self, text):
        self.page.get_by_text(text).click()


    def check(self, label):
        self.page.get_by_label(label).check()


    def upload_file_button(self, selector, file_path):
        self.page.get_by_role("button", name=selector, exact=True).click()
        self.page.get_by_role("button", name=selector, exact=True).set_input_files(file_path)

    def upload_file_re(self, selector, file_path):
        # print("selector=="+selector)
        # self.page.wait_for_selector(selector)
        self.page.locator("div").filter(has_text=selector).locator("button").click()
        self.page.locator("div").filter(has_text=selector).locator("button").set_input_files(file_path)

    def upload_filter(self, text ,file_path):
        self.page.locator('input[type="file"]').filter(has_text=text).set_input_files(file_path)

    def upload_file_by_index(self, index, file_path):
        print(index)
        print('=======')
        self.page.get_by_role("button", name="点击上传 /拖拽到此区域").nth(index).click()

        # self.page.locator('input[type="file"]').first.set_input_files(file_path)
        self.page.locator('input[type="file"]').nth(index).set_input_files(file_path)


        print(index)
        print(file_path)
        print('*********')

    def select(self, selector: str, name: str):
        self.page.get_by_text(selector).click()
        self.page.get_by_role("listitem", name=name).click()

        # self.page.get_by_text("公钥模式").click()
        # self.page.get_by_role('listitem', name=name).click()
        # self.page.get_by_role(selector, name=name).click()

    def click_fill(self, tag, content):
        self.page.get_by_placeholder(tag).click()
        self.page.get_by_placeholder(tag).fill(content)

    def fill(self, selector: str, text: str):
        """在指定的输入框中输入文本"""
        self.page.fill(selector, text)

    def screenshot(self, path: str):
        """对页面进行截图"""
        self.page.screenshot(path=path, full_page=True)

    def get_text(self, selector: str) -> str:
        """获取指定元素的文本内容"""
        return self.page.query_selector(selector).text_content()
